﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Web
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        private void DangKy(string HoTen_KH, bool GioiTinh, string CMND_KH, string NgaySinh_KH, string NgheNghiep, string DiaChi_KH, string SDT_KH, string Email_KH, string TenDangNhap, string MatKhau)
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from KhachHang where TenDangNhap_KH = @TenDangNhap_KH OR Email_KH = @Email_KH OR CMND_KH = @CMND_KH";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@TenDangNhap_KH", TenDangNhap);
            cmd.Parameters.AddWithValue("@Email_KH", Email_KH);
            cmd.Parameters.AddWithValue("@CMND_KH", CMND_KH);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                if ((string)reader["TenDangNhap_KH"] == TenDangNhap)
                    LblTenDangNhapError.Text = "Tên đăng nhập đã tồn tại";

                if ((string)reader["Email_KH"] == Email_KH)
                    LblEmailError.Text = "Email đã tồn tại";

                if ((string)reader["CMND_KH"] == CMND_KH)
                    LblCMNDError.Text = "Số CMND đã tồn tại";
                reader.Close();
            }
            else
            {
                reader.Close();
                cmd.CommandText = "insert into KhachHang(MaSo_KH,HoTen_KH,GioiTinh_KH,CMND_KH,NgaySinh_KH,NgheNghiep_KH,DiaChi_KH,SDT_KH,Email_KH,TenDangNhap_KH,MatKhau_KH) values('',@HoTen_KH,@GioiTinh_KH,@CMND_KH,@NgaySinh_KH,@NgheNghiep_KH,@DiaChi_KH,@SDT_KH,@Email_KH,@TenDangNhap_KH,@MatKhau_KH)";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@HoTen_KH", HoTen_KH);
                cmd.Parameters.AddWithValue("@GioiTinh_KH", GioiTinh);
                cmd.Parameters.AddWithValue("@CMND_KH", CMND_KH);
                cmd.Parameters.AddWithValue("@NgaySinh_KH", NgaySinh_KH);
                cmd.Parameters.AddWithValue("@NgheNghiep_KH", NgheNghiep);
                cmd.Parameters.AddWithValue("@DiaChi_KH", DiaChi_KH);
                cmd.Parameters.AddWithValue("@SDT_KH", SDT_KH);
                cmd.Parameters.AddWithValue("@Email_KH", Email_KH);
                cmd.Parameters.AddWithValue("@TenDangNhap_KH", TenDangNhap);
                cmd.Parameters.AddWithValue("@MatKhau_KH", MatKhau);
                cmd.ExecuteNonQuery();
                Response.Redirect("hoan-tat-dang-ky.aspx");
            }
            conn.Close();
        }

        protected void Button_Click(object sender, EventArgs e)
        {
            string NgaySinh = NgayDropDownList.SelectedValue + "/" + ThangDropDownList.SelectedValue + "/" + NamDropDownList.SelectedValue;
            bool gioitinh;
            if (GioTinhRadioButton.SelectedItem.Text == "Nam")
                gioitinh = true;
            else
                gioitinh = false;

            if (txtMatKhau.Text == txtMatKhau2.Text)
            {
                if (txtEmail.Text == txtEmail2.Text)
                {
                    DangKy(txtHoTen.Text, gioitinh, txtCMND.Text, NgaySinh, txtNgheNghiep.Text, txtDiaChi.Text, txtSDT.Text, txtEmail.Text, txtTenDangNhap.Text, txtMatKhau.Text);
                }
                else
                    LblEmailError.Text = "Email không đúng";
            }
            else
                LblMatKhauError.Text = "Mật khẩu không đúng";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("TrangChu.aspx");
        }
    }
}